﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FlashPay.Service.Impl
{
    using FlashPay.DAO.Interface;
    using FlashPay.EF.Models;
    using FlashPay.Entity;
    using FlashPay.Entity.Enum;
    using FlashPay.Entity.Parameter;
    using FlashPay.Entity.Request.Transfer;
    using FlashPay.Entity.Response.Company;
    using FlashPay.Entity.Response.Transfer;
    using FlashPay.Service.Interface;
    using FlashPay.Util;
    using System.Linq;

    /// <summary>
    /// 中转卡业务接口实现
    /// </summary>
    /// <remarks>2018-10-01 immi 创建</remarks>
    public class TransferServiceImpl:TransferService
    {
        /// <summary>
        /// 用户数据接口
        /// </summary>
        private readonly TransferDao _transferDao;

        /// <summary>
        /// 公司数据接口
        /// </summary>
        private readonly CompanyDao _companyDao;

        /// <summary>
        /// 银行数据接口
        /// </summary>
        private readonly BankDao _bankDao;

        /// <summary>
        /// 日志模型
        /// </summary>
        private readonly LogRecord _logRecord;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="transferDao"></param>
        public TransferServiceImpl(TransferDao transferDao, CompanyDao companyDao, BankDao bankDao, LogRecord logRecord)
        {
            this._transferDao = transferDao;
            this._companyDao = companyDao;
            this._bankDao = bankDao;
            this._logRecord = logRecord;
        }

        /// <summary>
        /// 根据编号获取公司
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>Company</returns>
        private List<CompanyResponse> GetCompany(int companyId, int level)
        {
            var companyRequest = new EF.Models.Company()
            {
                CompanyId = companyId,
                CompanyPid = -1,
                CompanyStatus = 1
            };

            var companyList = new BaseModel<List<EF.Models.Company>>();

            _companyDao.Get(companyList, companyRequest, level);
            var companyResponse = new List<CompanyResponse>();
            if (companyList.Success)
            {
                foreach (var item in companyList.Result)
                {
                    companyResponse.Add(new CompanyResponse()
                    {
                        CompanyID = item.CompanyId,
                        CompanyName = item.CompanyName,
                        CompanyStatus = item.CompanyStatus
                    });
                }
            }
            return companyResponse;
        }

        /// <summary>
        /// 获取中转卡
        /// </summary>
        /// <param name="query">查看参数</param>
        public DataGrid<TransferCardResponse> TransferCardPager(TransferCardQuery query)
        {
            query.CardType = new List<sbyte>()
            {
                (sbyte)CardType.中转卡.GetHashCode()
            };
            query.CompanyIds = _bankDao.GetCompanyIDList(query.CompanyId.Value);
            return _transferDao.TransferCardPager(query);
        }

        /// <summary>
        /// 获取中转卡记录
        /// </summary>
        /// <param name="query">查看参数</param>
        public DataGrid<TransferCardRecordResponse> TransferCardRecordPager(TransferCardRecordQuery query)
        {
            query.CompanyIds = _bankDao.GetCompanyIDList(query.CompanyId.Value);
            return _transferDao.TransferCardRecordPager(query);
        }

        /// <summary>
        /// 转失败
        /// </summary>
        /// <returns></returns>
        public JResult TurnFailed(TurnFailedReqeust request)
        {
            return _transferDao.TurnFailed(request);
        }

        /// <summary>
        /// 转成功
        /// </summary>
        /// <returns></returns>
        public JResult TurnSuccess(TurnSuccessReqeust request)
        {
            return _transferDao.TurnSuccess(request);
        }

        /// <summary>
        /// 重置
        /// </summary>
        /// <returns></returns>
        public JResult Reset(ResetReqeust request)
        {
            return _transferDao.Reset(request);
        }

        /// <summary>
        /// 中转卡记录重新付款
        /// </summary>
        /// <param name="request"></param>
        public JResult Repayment(RepaymentRequest request)
        {
            return _transferDao.Repayment(request);
        }

        /// <summary>
        /// 检查订单是否可以重新付款
        /// </summary>
        /// <param name="orderNo">订单号</param>
        /// <returns></returns>
        public JResult<TransportRecordResponse> VerifyRepayment(long orderNo)
        {
            return _transferDao.VerifyRepayment(orderNo);
        }
    }
}
